package com.cmcc.linlaker.ui.query;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.cmcc.linlaker.R;
import com.cmcc.linlaker.http.ApiDefine;
import com.cmcc.linlaker.ui.query.adapter.QueryFarmerAdapter;
import com.cmcc.linlaker.ui.query.bean.QueryProjectBean;
import com.cmcc.linlaker.ui.upload.NewUploadQuestionActivity;
import com.cmcc.linlaker.utils.BarUtils;
import com.cmcc.linlaker.utils.IntentUtils;
import com.cmcc.linlaker.utils.ToastUtils;
import com.cmcc.linlaker.utils.Utils;
import com.cmcc.linlaker.volley.GsonRequestHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HouseListActivity extends AppCompatActivity {

    @Nullable
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house_select);

        BarUtils.initStatusBar(this);

        initView();
    }

    private RecyclerView recycler_project;
    private QueryFarmerAdapter adapter;
    private List<QueryProjectBean.QueryProjectData> itemList = new ArrayList<>();
    private EditText edit_search;
    private RelativeLayout layout_clear;

    private void initView() {
        recycler_project = findViewById(R.id.recycler_project);
        setRecyclerView(recycler_project, itemList);

        edit_search = findViewById(R.id.edit_search);
        edit_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //关闭软键盘
                    Utils.hideSoftKeyboard(HouseListActivity.this, edit_search);
                    String keyName = edit_search.getText().toString();

                    getProjectSearchList(keyName, "", "", "");
                    return true;
                }
                return false;
            }
        });
        edit_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String keyName = edit_search.getText().toString();
                if (TextUtils.isEmpty(keyName)) {
                    getProjectSearchList("", "", "", "");
                }
            }
        });


        layout_clear = findViewById(R.id.layout_clear);
        layout_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit_search.setText("");
            }
        });


        getHouseList();
    }

    /**
     * 获取农民建房列表 search
     */
    public void getProjectSearchList(String houseName,
                                     String isControl,
                                     String nature,
                                     String form) {
        GsonRequestHelper helper = new GsonRequestHelper(this);
        Map<String, String> params = new HashMap<>();
        params.put("houseOwner", houseName);
        params.put("isControl", isControl);
        params.put("nature", nature);
        params.put("form", form);
        helper.sendPOSTRequest(ApiDefine.HOUSE_LIST, QueryProjectBean.class, params, false, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    QueryProjectBean bean = (QueryProjectBean) data;

                    if (bean.code.equals("0")) {
                        if (bean.rows != null && bean.rows.size() > 0) {
                            itemList.clear();
                            itemList.addAll(bean.rows);
                            adapter.notifyDataSetChanged();
                        } else {
                            itemList.clear();
                            adapter.notifyDataSetChanged();
                        }
                    } else if (bean.code.equals(IntentUtils.VAILD_CODE)) {
                        IntentUtils.skipToLogin(HouseListActivity.this);
                    } else {
                        ToastUtils.showText(HouseListActivity.this, bean.msg);
                    }
                }
            }
        });
    }


    private void setRecyclerView(final RecyclerView recycler_view, final List<QueryProjectBean.QueryProjectData> itemList) {
        recycler_view.setHasFixedSize(true);
        recycler_view.setLayoutManager(new LinearLayoutManager(this));

        recycler_view.setNestedScrollingEnabled(false);
        adapter = new QueryFarmerAdapter(this, itemList);
        adapter.setOnItemClickListener(new QueryFarmerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(HouseListActivity.this, NewUploadQuestionActivity.class);
                intent.putExtra("bean", itemList.get(position));
                setResult(2000, intent);
                finish();
            }
        });
        recycler_view.setAdapter(adapter);
    }

    /**
     * 获取项目列表
     */
    public void getHouseList() {
        GsonRequestHelper helper = new GsonRequestHelper(this);
        Map<String, String> params = new HashMap<>();

        helper.sendPOSTRequest(ApiDefine.HOUSE_LIST, QueryProjectBean.class, params, false, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    QueryProjectBean bean = (QueryProjectBean) data;

                    if (bean.code.equals("0")) {
                        if (bean.rows != null && bean.rows.size() > 0) {
                            itemList.clear();
                            itemList.addAll(bean.rows);
                            adapter.notifyDataSetChanged();
                        } else {
                            itemList.clear();
                            adapter.notifyDataSetChanged();
                        }
                    } else if (bean.code.equals(IntentUtils.VAILD_CODE)) {
                        IntentUtils.skipToLogin(HouseListActivity.this);
                    } else {
                        ToastUtils.showText(HouseListActivity.this, bean.msg);
                    }
                }
            }
        });
    }
}
